package com.ys.otms.controller;

import com.ys.otms.constant.WxConstant;
import com.ys.otms.services.ILoginServices;
import com.ys.otms.utils.StringUtil;
import com.ys.otms.websocket.WebSocket;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ILoginServices iLoginServices;

    @Autowired
    private WebSocket webSocket;

    @RequestMapping(value = "/loginV",method = RequestMethod.POST)
    @ResponseBody
    //手机验证码登录
    public JSONObject loginV(HttpServletRequest request) throws Exception {
        logger.info("LoginController loginV start ");
        JSONObject retObj;
        try {
            retObj = iLoginServices.loginV(request);
        } catch (Exception e) {
            logger.error("LoginController loginV Exception", e);
            return dealException(e);
        }
        return retObj;
    }

    @RequestMapping("/loginP")
    @ResponseBody
    //密码登录
    public JSONObject loginP(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) JSONObject obj) throws Exception {
        logger.info("LoginController loginP start ");
        JSONObject retObj;
        try {
            retObj = iLoginServices.loginP(request, response, obj);
        } catch (Exception e) {
            logger.error("LoginController loginP Exception", e);
            return dealException(e);
        }
        return retObj;
    }

    @RequestMapping("/wxLogin")
    @ResponseBody
    public String wxLogin(HttpServletRequest request) throws Exception {
        logger.info("LoginController wxLogin start ");
        String redirect_uri = "http%3a%2f%2fwww2.yihangotms.com%2fotms2%2fwxLoginCallBack.do";
        String state = StringUtil.getRandomString(10);
        request.getSession().setAttribute("LOGIN_STATE", state);
        String url = "https://open.weixin.qq.com/connect/qrconnect?appid=APP_ID&redirect_uri=REDIRECT_URI&scope=snsapi_login&state=STATE#wechat_redirect";
        return url.replaceAll("APP_ID", WxConstant.APP_ID).replaceAll("REDIRECT_URI",redirect_uri).replaceAll("STATE",state);
    }

    @RequestMapping("/wxLoginCallBack")
    public String wxLoginCallBack(HttpServletRequest request) throws Exception {
        logger.info("LoginController wxLoginCallBack start ");
        JSONObject retObj;
        try {
            retObj = iLoginServices.loginCallBack(request);
        } catch (Exception e) {
            logger.error("LoginController wxLoginCallBack Exception", e);
        }
        return "";
    }

    @RequestMapping("/wxLoginBind")
    @ResponseBody
    public JSONObject wxLoginBind(HttpServletRequest request) throws Exception {
        logger.info("LoginController wxLoginBind start ");
        JSONObject retObj = null;
        try {
            retObj = iLoginServices.wxLoginBind(request);
        } catch (Exception e) {
            logger.error("LoginController wxLoginBind Exception", e);
            return dealException(e);
        }
        return retObj;
    }

    @RequestMapping("/forgetPassword")
    @ResponseBody
    public JSONObject forgetPassword(HttpServletRequest request) throws Exception {
        logger.info("LoginController forgetPassword start ");
        JSONObject retObj;
        try {
            retObj = iLoginServices.forgetPassword(request);
        } catch (Exception e) {
            logger.error("LoginController forgetPassword Exception", e);
            return dealException(e);
        }
        return retObj;
    }

    @RequestMapping(value = "/sendVerification",method = RequestMethod.POST)
    @ResponseBody
    //发送验证码
    public JSONObject sendVerification(HttpServletRequest request) throws Exception {
        logger.info("LoginController sendVerification start ");
        JSONObject retObj;
        try {
            retObj = iLoginServices.sendVerification(request);
        } catch (Exception e) {
            logger.error("LoginController sendVerification Exception", e);
            return dealException(e);
        }
        return retObj;
    }

    private JSONObject dealException(Exception e) throws Exception {
        JSONObject retObj = new JSONObject();
        retObj.put("MSG","系统错误！");
        retObj.put("SUCCESS",false);
        return retObj;
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    //注册
    public JSONObject register(HttpServletRequest request) throws Exception {
        logger.info("LoginController register start ");
        JSONObject retObj;
        try {
            retObj = iLoginServices.register(request);
        } catch (Exception e) {
            logger.error("LoginController register Exception", e);
            return dealException(e);
        }
        return retObj;
    }

    @RequestMapping(value = "/get7Days",method = RequestMethod.GET)
    @ResponseBody
    //注册
    public JSONObject get7Days(HttpServletRequest request) throws Exception {
        logger.info("LoginController get7Days start ");
        JSONObject retObj;
        try {
            retObj = iLoginServices.get7Days(request);
        } catch (Exception e) {
            logger.error("LoginController get7Days Exception", e);
            return dealException(e);
        }
        return retObj;
    }


    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("LoginController logout  ");
        HttpSession session = request.getSession();
        session.invalidate();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("ys_user".equals(cookie.getName())) {
                cookie.setValue(null);
                cookie.setMaxAge(0);// 立即销毁cookie
                cookie.setPath("/");
                response.addCookie(cookie);
                break;
            }
        }
        response.sendRedirect("toLogin.do");
    }

}
